package com.lee.coupon.calculation.calcTemplate;

import com.lee.coupon.calculation.api.beans.ShoppingCart;
import com.lee.coupon.calculation.calcTemplate.impl.*;
import com.lee.coupon.template.api.beans.CouponInfo;
import com.lee.coupon.template.api.enums.CouponType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class CouponTemplateFactory {

    @Autowired
    private DiscountTemplate discountTemplate;

    @Autowired
    private DummyTemplate dummyTemplate;

    @Autowired
    private LonelyNightTemplate lonelyNightTemplate;

    @Autowired
    private MoneyOffTemplate moneyOffTemplate;

    @Autowired
    private RandomReductionTemplate randomReductionTemplate;

    public RuleTemplate getTemplate (ShoppingCart order) {

        List<CouponInfo> couponInfoList = order.getCouponInfos();
        if (CollectionUtils.isEmpty(couponInfoList)) {
            // dummy模板直接返回原价，不进行优惠计算
            log.debug("没有优惠券，返回原价");
            return dummyTemplate;
        }

        // 获取优惠券的类别
        // 目前每个订单只支持单张优惠券
        CouponType type = CouponType.convert(couponInfoList.get(0).getTemplate().getType());
        switch (type) {
            case MONEY_OFF: // 订单满减券
                return moneyOffTemplate;
            case DISCOUNT: // 打折券
                return discountTemplate;
            case RANDOM_DISCOUNT: // 随机立减券
                return randomReductionTemplate;
            case LONELY_NIGHT_MONEY_OFF: // 午夜下单优惠翻倍
                return lonelyNightTemplate;
            case ANTI_PUA:
            case UNKNOWN:
            default:
                return dummyTemplate;
        }



    }

}
